import pdb
import pyciv.action
import pyciv.resources

class Structure(object):
    def __init__(self, name, hex):
        self.Name = name
        self.Hex = hex
        
    def info(self):
        return ""

    def create_actions_for_hex(self, hex, engine):
        if ((hex == engine.current_hex) and 
            (engine.state.owner[hex].is_default_player() == True) and
            (engine.state.has_current_player_as_neighbour(hex))):
            return [pyciv.action.Influence(engine)]
        else:
            return [pyciv.action.Select()]

    def produce(self, engine):
        pass

    def consume(self, engine):
        pass

    def improve(self, engine):
        pass

    def _correct_owner(self, hex, engine):
        return ((engine.state.owner[hex] == engine.state.owner[self.Hex]) and (engine.state.owner[self.Hex] == engine.current_player()))

GROWTH_FACTOR = 10

class Village(Structure):
    def __init__(self, hex):
        Structure.__init__(self, "Village", hex)
        self.Population = 5
        self.Growth = 0

    def info(self):
        return "%d (%d)" % (self.Population, self.Growth)

    def create_actions_for_hex(self, hex, engine):
        actions = []
        if self._correct_owner(hex, engine):
            if self.Population > 1:
                if engine.state.structure[hex] == EMPTY:
                    actions.append(pyciv.action.Farm(self))
                    actions.append(pyciv.action.Market(self))
        else:
            pass
        return actions

    def produce(self, engine):
        engine.state.owner[self.Hex].resources[pyciv.resources.ACTION_POINTS] += self.Population

    def consume(self, engine):
        # Eat
        engine.state.owner[self.Hex].resources[pyciv.resources.FOOD] -= self.Population

    def improve(self, engine):
        # Growth
        if engine.state.owner[self.Hex].resources[pyciv.resources.FOOD] >= 0:
            self.Growth += self.Population
            if self.Growth >= GROWTH_FACTOR:
                self.Population += 1
                self.Growth -= GROWTH_FACTOR
            engine.state.owner[self.Hex].resources[pyciv.resources.INFLUENCE] += self.Population
        else:
            self.Growth -= self.Population
            if self.Growth < 0:
                self.Growth = 0
                self.Population -= 1
    
class Farm(Structure):
    def __init__(self, hex):
        Structure.__init__(self, "Farm", hex)
        self.level = 2

    def info(self):
        return str(self.level)

    def create_actions_for_hex(self, hex, engine):
        actions = []
        if self._correct_owner(hex, engine):
            if isinstance(engine.state.structure[hex], Village):
                actions.append(pyciv.action.Abandon(self.Hex))
        else:
            pass
        return actions

    def produce(self, engine):
        engine.state.owner[self.Hex].resources[pyciv.resources.FOOD] += self.level

    def improve(self, engine):
        if self.level < 4:
            self.level += 1
        engine.state.owner[self.Hex].resources[pyciv.resources.INFLUENCE] += 1

class Market(Structure):
    def __init__(self, hex):
        Structure.__init__(self, "Market", hex)
        self.level = 1

    def info(self):
        return str(self.level)

    def create_actions_for_hex(self, hex, engine):
        actions = []
        if self._correct_owner(hex, engine):
            if isinstance(engine.state.structure[hex], Village):
                actions.append(pyciv.action.Abandon(self.Hex))
        else:
            pass
        return actions

    def produce(self, engine):
        engine.state.owner[self.Hex].resources[pyciv.resources.GOLD] += self.level

    def consume(self, engine):
        engine.state.owner[self.Hex].resources[pyciv.resources.FOOD] -= 1

    def improve(self, engine):
        if self.level < 3:
            self.level += 1

        engine.state.owner[self.Hex].resources[pyciv.resources.INFLUENCE] += 1


EMPTY = Structure("-", None)
